<template>
  <div>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="3">
          <el-input v-model="columnName" placeholder="分类名"></el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addColumn">
            <svg-icon name="add" width="20px" height="20px" style="padding-right: 5px;"></svg-icon> 添加
          </el-button>
        </el-col>
        <el-col :span="3">
          <el-input v-model="name" placeholder="请输入栏目名称"></el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="searchColumn">
            <svg-icon name="search" width="20px" height="20px" style="padding-right: 5px;"></svg-icon> 搜索
          </el-button>
        </el-col>
      </el-row>
      <!-- 水平线 -->
      <el-divider></el-divider>
      <el-row>
        <el-col :span="24">
        </el-col>
        <el-col :span="24">
          <div class="explain">
            <span></span>
            <p>
              每个tr上有两个属性cate-id="1"当前分类id fid="0" 父级id,顶级分类为0,有子分类的前面加收缩图标
            </p>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <el-button type="danger" style="margin-top: 20px;" @click="deleteColumn">
            <svg-icon name="delete" width="20px" height="20px" style="padding-right: 5px;"></svg-icon>
            批量删除</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <!-- 树形表格  -->
          <el-table :data="columnStore.columnList" stripe border row-key="id" :tree-props="{ children: 'children' }"
            style="margin-top: 30px;" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="100" :reserve-selection="true" />
            <el-table-column type="" prop="id" label="ID" width="100" />
            <el-table-column prop="name" label="栏目名" width="600">
            </el-table-column>
            <el-table-column prop="sort" label="排序" width="110" sortable>
              <template #default="scope">
                <el-input disabled v-model="scope.row.sort" placeholder="Please input" />
              </template>

            </el-table-column>
            <el-table-column prop="status" label="状态" width="110">
              <template #default="scope">
                <el-switch disabled v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" />
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <el-button type="primary" size="small" @click="addSubColumn(scope.row)">
                  <svg-icon name="add" plain width="20px" height="15px" style="padding-right: 5px;"></svg-icon>
                  添加子栏目</el-button>
                <el-button type="primary" size="small" @click="editColumnById(scope.row)">
                  <svg-icon name="pen" width="20px" height="10px" style="padding-right: 5px;"></svg-icon>
                  编辑</el-button>
                <el-popconfirm width="220" :icon="InfoFilled" icon-color="#626AEF" :title="`确认删除 ${scope.row.name} 吗？`"
                  @confirm="deleteColumnById(scope.row)">
                  <template #reference>
                    <el-button type="danger" size="small">
                      <svg-icon name="delete" width="20px" height="15px" style="padding-right: 5px;"></svg-icon>
                      删除</el-button>
                  </template>
                  <template #actions="{ confirm, cancel }">
                    <el-button size="small">不!</el-button>
                    <el-button type="danger" size="small" @click="confirm">
                      确认？
                    </el-button>
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-dialog v-model="dialogFormVisible" :title="title" width="500" :before-close="cancel">
        <el-form :model="form" :rules="rules">
          <el-form-item label="栏目名称" label-width="100" label-position="top" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="排序" label-width="100" label-position="top">
            <el-input v-model="form.sort"></el-input>
          </el-form-item>
          <el-form-item label="状态" label-width="100" label-position="top">
            <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="saveOrUpdata">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
      <!-- 分类器 -->
      <!-- 分页器 -->
      <el-pagination @current-change="searchColumn" @size-change="searchColumn" size="small" v-model:current-page="pageNo"
        v-model:page-size="limit" :page-sizes="[3, 5, 10, 20]" :total="total" :background="true"
        layout="prev, pager, next, jumper,->,sizes, total" style="margin-top: 20px;text-align: center;">
      </el-pagination>

    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue"
import useColumnStore from "@/store/modules/column"
import { ElMessage } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'
let columnName = ref('')

let title = ref("")
let pageNo = ref(1)
let limit = ref(3)
let deleteTitle = ref('')
let total = ref(0)

let name = ref('')

const dialogFormVisible = ref(false)

const multipleTableRef = ref()
const select_order_number = ref('') //表格select选中的条数
const select_orderId = ref([]) //表格select复选框选中的id
const multipleSelection = ref([])


const form = reactive({
  id: null,
  name: '',
  sort: 1,
  status: true,
  pid: null
})



const columnStore = useColumnStore()
onMounted(() => {
  getColumn()
})

const getColumn = () => {
  columnStore.getColumnList(0, pageNo.value, limit.value).then(res => {
  })
  columnStore.getColumnNumber().then(res => {
    total.value = res
  })
}

const addSubColumn = (row) => {
  form.pid = row.id
  title.value = '添加子栏目'
  dialogFormVisible.value = true
}

const addColumn = () => {
  if (columnName.value === '') {
    ElMessage.error('请输入栏目名称')
    return
  }
  columnStore.addColumnName(columnName.value).then(res => {
    if (res === "ok") {
      ElMessage.success('添加成功')
      getColumn()
      columnName.value = ''

    } else {
      ElMessage.error('添加失败')
    }

  })
}

const cancel = () => {
  dialogFormVisible.value = false
  form.name = ''
  form.sort = 1
  form.status = true
  form.pid = null
}

const saveOrUpdata = () => {
  if (title.value === '添加子栏目') {
    if (form.name === '') {
      ElMessage.error('请输入栏目名称')
      return
    }

    columnStore.addSubColumn(form).then(res => {
      if (res === "ok") {
        ElMessage.success('添加成功')
        getColumn()
        dialogFormVisible.value = false
        form.name = ''
        form.sort = 1
        form.status = null
        form.pid = null
      } else {
        ElMessage.error('添加失败')
      }
    })
  } else {
    columnStore.updateColumn(form).then(res => {
      if (res === "ok") {
        ElMessage.success('修改成功')
        getColumn()
        dialogFormVisible.value = false
        form.id = null
        form.name = ''
        form.sort = 1
        form.status = true
        form.pid = null
      } else {
        ElMessage.error('修改失败')
      }
    })
  }
}

//选中的list
const getRowKeys = (row) => {
  //记录每行的key值
  return row.id;
}
//当表格选择项发生变化时会触发该事件
const handleSelectionChange = (val) => {
  // 解决来回切换页面，也无法清除上次选中情况
  multipleSelection.value = val;
  select_order_number.value = multipleSelection.value.length;
  select_orderId.value = [];
  if (val) {
    undefined;
    val.forEach((row) => {
      undefined;
      if (row) {
        undefined;
        select_orderId.value.push(row.id);
      }
    });
  }
  console.log(select_orderId.value);
}
//删除批量删除
const deleteColumn = () => {
  if (select_orderId.value.length === 0) {
    ElMessage.error('请选择要删除的数据')
    return
  }
  columnStore.deleteColumns(select_orderId.value).then(res => {
    if (res === "ok") {
      ElMessage.success('删除成功')
      getColumn()
    } else {
      ElMessage.error('删除失败')
    }
  })
}

const deleteColumnById = (row) => {
  columnStore.deleteColumns([row.id]).then(res => {
    if (res === "ok") {
      ElMessage.success('删除成功')
      getColumn()
    } else {
      ElMessage.error('删除失败')
    }
  })
}

const editColumnById = (row) => {
  title.value = '编辑栏目'
  dialogFormVisible.value = true;
  form.id = row.id
  form.name = row.name
  form.sort = row.sort
  form.status = row.status
  form.pid = row.pid
}


const rules = reactive({
  name: {
    required: true, message: '请输入栏目名称', trigger: 'blur'
  },
})

const searchColumn = () => {
  columnStore.searchColumn(name.value, pageNo.value, limit.value)
  columnStore.searchColumnNumber(name.value).then(res => {
    console.log(res);
    total.value = res
  })
}


</script>

<style lang="scss" scoped>
.explain {
  background-color: LightGray;
  color: black;
  font-size: 14px;
  width: 100%;
  height: 50px;
  // 子元素垂直居中
  display: flex;
  align-items: center;
  // 子元素水平居中
  justify-content: left;

  span {
    width: 10px;
    height: 100%;
    background-color: DarkTurquoise;
    margin-right: 10px
  }

}
</style>
